package logic

import (
	"context"
	"encoding/json"
	"furnipack/furnipackrpc/furnipackrpc"
	"furnipack/furnipackrpc/internal/svc"
	"furnipack/model/cache"
	"furnipack/model/mysql"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type FurnListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFurnListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FurnListLogic {
	return &FurnListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *FurnListLogic) FurnList(in *furnipackrpc.FurnListRequest) (*furnipackrpc.FurnListResponse, error) {
	// todo: add your logic here and delete this line
	var list []*furnipackrpc.FurnList
	result, _ := cache.Rdb.Get(l.ctx, "furn").Result()
	if result == "" {
		list, _ = mysql.FurnList(mysql.DB)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(l.ctx, "furn", jsonData, time.Minute*60)
	} else {
		json.Unmarshal([]byte(result), &list)
	}

	return &furnipackrpc.FurnListResponse{
		List: list,
	}, nil
}
